package com.wj.jiangan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.mapper.ProductMapper;
import com.wj.jiangan.pojo.Product;
import com.wj.jiangan.pojo.vo.productStockSortVo;
import com.wj.jiangan.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    public ProductMapper productMapper;

    @Override
    public List<Product> selectAllProduect() {
        return productMapper.selectList(null);
    }


    /**
     * 分页查询所有
     *
     * @param page
     * @return
     */
    @Override
    public IPage<Product> findProductByPage(Page<Product> page) {
        QueryWrapper<Product> product = new QueryWrapper<>();
        return productMapper.selectPage(page, product);
    }



    /**
     * 下架商品查询
     *
     * @param page
     * @param param
     * @return
     */
    @Override
    public IPage<Product> productByProductpIsUppxia(Page<Product> page, Map<String, Object> param) {
        Integer pis = (Integer) param.get("pis");
        return productMapper.productByProductpIsUppxia(page, pis);
    }

    //查询库存商品的信息
    @Override
    public IPage<productStockSortVo> findpssvoPage(Page page, Map<String, Object> param) {
        Integer sid = (Integer) param.get("sid");
        Integer pid = (Integer) param.get("pid");
        return productMapper.findAllpssvoinfo(page, sid, pid);
    }



    @Override
    public List<Map<String, Object>> getProdSales(Integer sid) {
        return productMapper.getProdSales(sid);

    }

    //添加商品2
    @Override
    public List<Map<String, Object>> getProductSid(Map<String,Object> param) {
        Integer sid=(Integer) param.get("sid");
        return productMapper.selectProduct(sid);
    }

    /**
     * 添加商品
     *
     * @param product
     * @return
     */
    @Override
    public int saveProduct(Product product ) {
        product.setPIs(1);
        return productMapper.insert(product);
    }

    /**
     * 根据id查询所有商品
     * @param queryWrapper
     * @return
     */
    @Override
    public List<Product> queryPoductByPId(QueryWrapper queryWrapper) {
        return productMapper.selectList(queryWrapper);
    }

    /**
     * 修改商品
     * @param product
     */
    @Override
    public void updateProductByPId(Product product) {
        productMapper.updateById(product);
    }

    //用于库存管理三级联动获取id
    @Override
    public List<Product> findproductvoAll(int pid) {
        QueryWrapper<Product> pqw = new QueryWrapper<>();
        pqw.eq("p_id", pid);
        return productMapper.selectList(pqw);
    }

    @Override
    public List<Product> getExistProductBySid(Integer sid) {
        QueryWrapper<Product> pqw = new QueryWrapper<>();
        pqw.eq("p_is", 1);
        pqw.eq("s_id", sid);
        return productMapper.selectList(pqw);
    }

    /**
     * 获取产品的所有产地
     *
     * @param pname
     * @return
     */
    @Override
    public List<Map<String, Object>> getProductAreaByPName(String pname) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("p_is", 1);
        qw.eq("p_name", pname);
        return  productMapper.selectList(qw).stream().map(p -> {
            Map<String, Object> map = new HashMap<>();
            map.put("pid", p.getPId());
            map.put("pArea", p.getPArea());
            return map;
        }).collect(Collectors.toList());
    }



}
